package com.woniuxy.info.replies.controller;

import com.woniuxy.info.replies.client.InfoApi;
import com.woniuxy.info.sys.entity.Informations;
import com.woniuxy.info.sys.entity.Replies;
import com.woniuxy.info.sys.model.RepliesDto;
import com.woniuxy.info.sys.result.Result;
import com.woniuxy.info.sys.service.IRepliesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论回复控制器
 */
@RestController
@RequestMapping
@Slf4j
@Api(tags = "评论控制器")
public class RepliesController {

    @Autowired
    private IRepliesService repliesService;
    @Autowired
    private InfoApi infoApi;

    @ApiOperation("分页查询资讯列表")
    @GetMapping("/queryList")
    public Result queryInfoList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer size){
        Result result = infoApi.queryInfoList(current, size);
        return result;
    }

    @ApiOperation("资讯详情")
    @GetMapping("/info/{id}")
    public Result<Informations> findInfoById(
            @PathVariable("id")
            @ApiParam(name ="id",value ="资讯ID",required = true) Integer id){
        Result<Informations> info = infoApi.findInfoById(id);
        return info;
    }
    @PutMapping("/reply/count/{infoId}")
    @ApiOperation("修改资讯评论数量")
    Result<Integer> updateReplyCount(
            @PathVariable("infoId")
            @ApiParam(name ="infoId",value ="资讯ID",required = true) Integer infoId){
        Result<Integer> integerResult = infoApi.updateReplyCount(infoId);
        return integerResult;
    }

    @PostMapping("/rely/save")
    @ApiOperation("保存评论")
    public Result saveReplies(@RequestBody RepliesDto repliesDto) {

        repliesService.saveReplies(repliesDto);
        infoApi.updateReplyCount(repliesDto.getInfoId());
        return Result.ok();
    }


    @GetMapping("/list/{infoId}")
    @ApiOperation("查询评论信息")
    public Result list(@PathVariable("infoId") Integer infoId) {
        List<Replies> list = repliesService.findRepliesByInfoId(infoId);
        return Result.ok(list);
    }
}
